package com.network.service.common;

public class ApiResultGenerator {
    /**
     * 执行成功后不带返回内容
     *
     * @return
     */
    public static BaseResult success() {
        BaseResult baseResult = BaseResult.newInstance();
        baseResult.setErrMsg("");
        baseResult.setErrCode(1);
        baseResult.setData("");
        return baseResult;
    }

    /**
     * 执行成功后的返回内容
     *
     * @return
     */
    public static BaseResult successResult(Object result) {
        BaseResult apiBaseResult = BaseResult.newInstance();
        apiBaseResult.setErrMsg("");
        apiBaseResult.setErrCode(1);
        apiBaseResult.setData(result);
        return apiBaseResult;
    }

    /**
     * 执行成功后的返回内容
     *
     * @return
     */
    public static PageResult successWithResult(Object result) {
        PageResult pageResult = PageResult.newInstance();
        pageResult.setErrMsg("");
        pageResult.setErrCode(1);
        pageResult.setSize(0);
        pageResult.setTotal(0);
        pageResult.setPageNum(0);
        pageResult.setPages(0);
        pageResult.setPageSize(0);
        pageResult.setData(result);
        return pageResult;
    }

    /**
     * 执行成功后的返回内容并附带结果条目信息
     *
     * @return
     */
    public static PageResult successByPage(CustomPage customPage) {
        PageResult pageResult = PageResult.newInstance();
        pageResult.setErrMsg("");
        pageResult.setErrCode(1);
        pageResult.setSize(customPage.getData().size());
        pageResult.setTotal(Integer.parseInt(String.valueOf(customPage.getTotal())));
        pageResult.setPageNum(customPage.getPageNum());
        pageResult.setPages(Integer.parseInt(String.valueOf(customPage.getPages())));
        pageResult.setPageSize(customPage.getPageSize());
        pageResult.setData(customPage.getData());
        return pageResult;
    }

    /**
     * 执行成功后的返回内容并附带结果条目信息
     *
     * @return
     */
    public static PageResult successByPage(CustomPage customPage, Object result) {
        PageResult pageResult = PageResult.newInstance();
        pageResult.setErrMsg("");
        pageResult.setErrCode(1);
        pageResult.setSize(customPage.getData().size());
        pageResult.setTotal(Integer.parseInt(String.valueOf(customPage.getTotal())));
        pageResult.setPageNum(customPage.getPageNum());
        pageResult.setPages(Integer.parseInt(String.valueOf(customPage.getPages())));
        pageResult.setPageSize(customPage.getPageSize());
        pageResult.setData(result);
        return pageResult;
    }

    /**
     * 执行失败
     *
     * @return
     */
    public static BaseResult error(Integer errorCode, String errorMsg) {
        BaseResult apiBaseResult = BaseResult.newInstance();
        apiBaseResult.setErrMsg(errorMsg);
        apiBaseResult.setErrCode(errorCode);
        apiBaseResult.setData("");
        return apiBaseResult;
    }

    /**
     * 执行失败
     *
     * @return
     */
    public static BaseResult error(Integer errorCode, String errorMsg, Object result) {
        BaseResult apiBaseResult = BaseResult.newInstance();
        apiBaseResult.setErrMsg(errorMsg);
        apiBaseResult.setErrCode(errorCode);
        apiBaseResult.setData(result);
        return apiBaseResult;
    }
}
